X-Git-Url: https://git.r.bdr.sh/rbdr/super-polarity/blobdiff_plain/95d7601b7742ed560a9d8e00269217f62fc7ce32..b587e9d8e0cc5eb1edf972fd3b644704441e5289:/Super%20Polarity/InputController.cs diff --git a/Super Polarity/InputController.cs b/Super Polarity/InputController.cs index 405b3ab..c92bb9c 100644 --- a/Super Polarity/InputController.cs +++ b/Super Polarity/InputController.cs @@ -11,6 +11,8 @@ namespace SuperPolarity static Dictionary>> Listeners; static Dictionary> RegisteredKeys; static Dictionary> RegisteredButtons; + static List BlockedKeys; + static List BlockedButtons; static GamePadState InputGamePadState; static KeyboardState InputKeyboardState; @@ -25,25 +27,134 @@ namespace SuperPolarity static InputController() { Listeners = new Dictionary>>(); + RegisteredButtons = new Dictionary>(); + RegisteredKeys = new Dictionary>(); + BlockedKeys = new List(); + BlockedButtons = new List(); InputKeyboardState = new KeyboardState(); InputGamePadState = new GamePadState(); } public static void UpdateInput() { - Poll(); DispatchMoveEvents(); DispatchRegisteredEvents(); } + public static void UpdateInput(bool highPriorityOnly) + { + Poll(); + DispatchPauseEvent(); + if (!highPriorityOnly) + { + UpdateInput(); + } + } + + public static void DispatchPauseEvent() + { + // OK THIS IS ALL KINDS OF WRONG. THIS IS A PLACEHOLDER BECAUSE DEMO! + var keyPressed = false; + if ((InputKeyboardState.IsKeyDown(Keys.Enter) || InputGamePadState.IsButtonDown(Buttons.Start))) { + keyPressed = true; + if(!BlockedButtons.Contains("pause") && !BlockedKeys.Contains("pause")) + { + BlockedButtons.Add("pause"); + BlockedKeys.Add("pause"); + Console.WriteLine("Dispatch"); + Dispatch("pause", 0); + } + } + + if (!keyPressed) + { + BlockedButtons.Remove("pause"); + BlockedKeys.Remove("pause"); + } + } + private static void Poll() { InputGamePadState = GamePad.GetState(Microsoft.Xna.Framework.PlayerIndex.One); InputKeyboardState = Keyboard.GetState(); } + public static void RegisterEventForKey(string eventName, Keys key) + { + List newKeyList; + if (!RegisteredKeys.ContainsKey(eventName)) + { + newKeyList = new List(); + RegisteredKeys.Add(eventName, newKeyList); + } + + RegisteredKeys.TryGetValue(eventName, out newKeyList); + + newKeyList.Add(key); + } + + public static void RegisterEventForButton(string eventName, Buttons button) + { + List newButtonList; + if (!RegisteredButtons.ContainsKey(eventName)) + { + newButtonList = new List(); + RegisteredButtons.Add(eventName, newButtonList); + } + + RegisteredButtons.TryGetValue(eventName, out newButtonList); + + newButtonList.Add(button); + } + private static void DispatchRegisteredEvents() { + var keyFired = false; + + foreach (KeyValuePair> entry in RegisteredKeys) { + keyFired = false; + foreach (Keys key in entry.Value) + { + if (InputKeyboardState.IsKeyDown(key)) + { + if (!BlockedKeys.Contains(entry.Key)) + { + BlockedKeys.Add(entry.Key); + Dispatch(entry.Key, 1); + } + keyFired = true; + break; + } + } + + if (!keyFired) + { + BlockedKeys.Remove(entry.Key); + } + } + + foreach (KeyValuePair> entry in RegisteredButtons) + { + keyFired = false; + foreach (Buttons button in entry.Value) + { + if (InputGamePadState.IsButtonDown(button)) + { + if (!BlockedButtons.Contains(entry.Key)) + { + BlockedButtons.Add(entry.Key); + Dispatch(entry.Key, 1); + } + keyFired = true; + break; + }; + } + + if (!keyFired) + { + BlockedButtons.Remove(entry.Key); + } + } } private static void DispatchMoveEvents() @@ -55,8 +166,6 @@ namespace SuperPolarity xMovement = InputGamePadState.ThumbSticks.Left.X; yMovement = -InputGamePadState.ThumbSticks.Left.Y; - Console.WriteLine("Dispatching Input {0}", InputKeyboardState.IsKeyDown(Keys.Left)); - if (InputKeyboardState.IsKeyDown(Keys.Left)) { xMovement = -1.0f; @@ -97,6 +206,21 @@ namespace SuperPolarity listenerList.Add(listener); } + public static void Unbind(string eventName, Action listener) + { + List> listenerList; + bool foundListeners; + + if (!Listeners.ContainsKey(eventName)) + { + return; + } + + foundListeners = Listeners.TryGetValue(eventName, out listenerList); + + listenerList.Remove(listener); + } + public static void Dispatch(string eventName, float value) { List> listenerList; @@ -109,10 +233,16 @@ namespace SuperPolarity return; } - foreach (Action method in listenerList) + for (var i = listenerList.Count - 1; i >= 0; i--) { - method(value); + listenerList[i](value); } } + + public static void Unlock(string eventName) + { + BlockedButtons.Remove(eventName); + BlockedKeys.Remove(eventName); + } } }